/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2015年1月7日         Root.Lu
 */

package com.coocaa.x.framework.app;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;

import com.coocaa.x.framework.utils.LiteHandlerThread;
import com.coocaa.x.framework.utils.LogUtils;

public abstract class CoocaaService extends Service {
    private static final String TAG = "CoocaaService";

    public static class AIDLInterface<T> {
        public IBinder binder = null;
        public T i = null;

        public AIDLInterface(IBinder binder, T i) {
            this.binder = binder;
            this.i = i;
        }
    }

    private LiteHandlerThread ht = null;
    private static Context mContext = null;

    public static Context getContext() {
        return mContext;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        if (mContext == null)
            mContext = this;
//        ht = new LiteHandlerThread(getName());
//        ht.start();
        LogUtils.d(TAG, getName() + " onCreate");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (ht != null) {
            ht.quit();
            ht = null;
        }
        LogUtils.d(TAG, getName() + " onDestroy");
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public boolean onUnbind(Intent intent) {
        return super.onUnbind(intent);
    }

    public final void post(Runnable r) {
//        ht.post(r);
        CoocaaApplication.post(r);
    }

//    public final void postDelayed(Runnable r, long delay) {
////        ht.postDelayed(r, delay);
//    }

    protected final synchronized void postOnSingleThread(Runnable r) {
        if (ht == null) {
            ht = new LiteHandlerThread(getName());
            ht.start();
        }
        ht.post(r);
    }

    protected String getName() {
        return getClass().getName();
    }
}
